$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/sysgzh2/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id',align: "center", width: 50, key: true ,formatter:function(value,options,rowData){
					var userId=rowData["id"];
					return "<a  onclick='update("+value+")'>"+userId+"</a>";
					}
				},
			{ label: '公众号标识', name: 'gzhCode', index: 'gzh_code',align: "center", width: 80 },
			{ label: '公众号名称', name: 'gzhName', index: 'gzh_name',align: "center", width: 80 },
			{ label: '本月粉丝目标数量', name: 'currentMonthFansCount', index: 'current_month_fans_count',align: "center", width: 80 },
			{ label: '操作', name: '', index: '',align: "center", width: 80,
				formatter(value,options,rowData) {
					return "<a onclick='vm.setCurrentMonthFollow(\""+rowData['gzhCode']+"\",\""+rowData['gzhName']+"\")'>设置</a>";
				}
			},

        ],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
function update(e) {
	vm.showList = false;
	vm.title = "修改";

	vm.getInfo(e);
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sysgzh2: {},
		currentGzhCode: null,
		currentGzhName: null,
		index: null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sysgzh2 = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.sysgzh2.id == null ? "sys/sysgzh2/save" : "sys/sysgzh2/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.sysgzh2),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
		// 	confirm('确定要删除选中的记录？', function(){
		// 		$.ajax({
		// 			type: "POST",
		// 		    url: baseURL + "sys/buserpromotionpaymentinfo/delete",
        //             contentType: "application/json",
		// 		    data: JSON.stringify(ids),
		// 		    success: function(r){
		// 				if(r.code == 0){
		// 					alert('操作成功', function(index){
		// 						$("#jqGrid").trigger("reloadGrid");
		// 					});
		// 				}else{
		// 					alert(r.msg);
		// 				}
		// 			}
		// 		});
		// 	});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/sysgzh2/info/"+id, function(r){
                vm.sysgzh2 = r.sysgzh2;
            });
		},
		setCurrentMonthFollow: function(gzhCode, gzhName) {
			// debugger
			vm.currentGzhCode = gzhCode;
			vm.currentGzhName = gzhName;
			let a1 = vm.currentGzhCode;
			let a2 = vm.currentGzhName;

			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			$("#currentMonth").val(year + "年" + month + "月");
			$("#fansCount").val("");

			var index = layer.open({
				type: 1,
				title: ['设置本月粉丝目标数量', 'font-weight: bold'],
				area: ['560px', '320px'],
				shadeClose: true,
				scrollbar: false,
				content: $('#layerSet')
			});
			vm.index = index;
		},
		// 更新公众号的本月目标粉丝数量
		updateGzhFollow: function() {
			// 本月目标粉丝数量
			let currentMonthFansCount = $("#fansCount").val();
			$.ajax({
				type: "POST",
				url: baseURL + "sys/sysgzh2/updateFansCount",
				contentType: "application/json",
				data: JSON.stringify({'gzhCode':vm.currentGzhCode, 'currentMonthFansCount':currentMonthFansCount}),
				success: function(r){
					if(r.code == 0){
						// alert('设置成功');
						layer.close(vm.index);
						vm.reload();
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});